from mcp.server.fastmcp import FastMCP, Context
import subprocess
import json
from typing import List

mcp = FastMCP("Xorg Synaptics MCP Server")
"""
面向 xorg-x11-drv-synaptics 软件的 MCP Server

提供的 MCP tools：
1. synclient_list_settings - 列出当前触摸板用户设置
   参数：无
   返回：命令执行结果 JSON

2. synclient_version - 显示 synclient 版本信息
   参数：无
   返回：命令执行结果 JSON

3. synclient_set_parameters - 设置触摸板参数
   参数：parameters - 参数列表，如 ["TapButton1=1", "TapButton2=2"]
   返回：命令执行结果 JSON

4. syndaemon_start - 启动触摸板守护进程
   参数：interval - 检测间隔秒数，monitor_interval - 监控间隔毫秒数，pid_file - PID 文件路径
   返回：命令执行结果 JSON

5. syndaemon_help - 显示守护进程帮助信息
   参数：无
   返回：命令执行结果 JSON

所有工具返回统一 JSON 结构：
{
  "success": bool,
  "command": str,
  "exit_code": int,
  "stdout": str,
  "stderr": str
}
"""

@mcp.tool()
def synclient_list_settings() -> dict:
    """
    列出当前触摸板用户设置
    
    返回:
        dict: 包含命令执行结果的 JSON 对象
    """
    try:
        result = subprocess.run(
            ["synclient", "-l"],
            capture_output=True,
            text=True,
            timeout=30
        )
        return {
            "success": result.returncode == 0,
            "command": "synclient -l",
            "exit_code": result.returncode,
            "stdout": result.stdout,
            "stderr": result.stderr
        }
    except Exception as e:
        return {
            "success": False,
            "command": "synclient -l",
            "exit_code": -1,
            "stdout": "",
            "stderr": str(e)
        }

@mcp.tool()
def synclient_version() -> dict:
    """
    显示 synclient 版本信息
    
    返回:
        dict: 包含命令执行结果的 JSON 对象
    """
    try:
        result = subprocess.run(
            ["synclient", "-V"],
            capture_output=True,
            text=True,
            timeout=30
        )
        return {
            "success": result.returncode == 0,
            "command": "synclient -V",
            "exit_code": result.returncode,
            "stdout": result.stdout,
            "stderr": result.stderr
        }
    except Exception as e:
        return {
            "success": False,
            "command": "synclient -V",
            "exit_code": -1,
            "stdout": "",
            "stderr": str(e)
        }

@mcp.tool()
def synclient_set_parameters(parameters: List[str]) -> dict:
    """
    设置触摸板参数
    
    参数:
        parameters: 参数列表，例如 ["TapButton1=1", "TapButton2=2"]
    
    返回:
        dict: 包含命令执行结果的 JSON 对象
    """
    try:
        command = ["synclient"] + parameters
        result = subprocess.run(
            command,
            capture_output=True,
            text=True,
            timeout=30
        )
        return {
            "success": result.returncode == 0,
            "command": " ".join(command),
            "exit_code": result.returncode,
            "stdout": result.stdout,
            "stderr": result.stderr
        }
    except Exception as e:
        return {
            "success": False,
            "command": "synclient " + " ".join(parameters),
            "exit_code": -1,
            "stdout": "",
            "stderr": str(e)
        }

@mcp.tool()
def syndaemon_start(interval: int = 1, monitor_interval: int = 100, pid_file: str = "./syndaemon.pid") -> dict:
    """
    启动触摸板守护进程
    
    参数:
        interval: 检测间隔秒数，默认 1
        monitor_interval: 监控间隔毫秒数，默认 100
        pid_file: PID 文件路径，默认 "./syndaemon.pid"
    
    返回:
        dict: 包含命令执行结果的 JSON 对象
    """
    try:
        command = [
            "syndaemon",
            "-i", str(interval),
            "-m", str(monitor_interval),
            "-d",
            "-p", pid_file,
            "-t", "-k", "-K", "-R", "-v"
        ]
        result = subprocess.run(
            command,
            capture_output=True,
            text=True,
            timeout=30
        )
        return {
            "success": result.returncode == 0,
            "command": " ".join(command),
            "exit_code": result.returncode,
            "stdout": result.stdout,
            "stderr": result.stderr
        }
    except Exception as e:
        return {
            "success": False,
            "command": "syndaemon",
            "exit_code": -1,
            "stdout": "",
            "stderr": str(e)
        }

@mcp.tool()
def syndaemon_help() -> dict:
    """
    显示守护进程帮助信息
    
    返回:
        dict: 包含命令执行结果的 JSON 对象
    """
    try:
        result = subprocess.run(
            ["syndaemon", "-?"],
            capture_output=True,
            text=True,
            timeout=30
        )
        return {
            "success": result.returncode == 0,
            "command": "syndaemon -?",
            "exit_code": result.returncode,
            "stdout": result.stdout,
            "stderr": result.stderr
        }
    except Exception as e:
        return {
            "success": False,
            "command": "syndaemon -?",
            "exit_code": -1,
            "stdout": "",
            "stderr": str(e)
        }

if __name__ == "__main__":
    mcp.run()